package com.dfch.auto.appium.app;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.time.Duration;

/**
 * @author ldcmaxim on 05/04/2018.
 */
public abstract class AutoApp {

    /**
     * 初始化
     *
     * @param deviceName      设备名称
     * @param platformVersion 版本号
     * @param url             连接地址
     */
    public abstract boolean init(String deviceName, String platformVersion, String url);

    /**
     * 执行脚本
     *
     * @param script json脚本
     * @param times  循环次数
     */
    public abstract boolean execute(String script, int times);

    /**
     * 停止,资源释放
     */
    public abstract void release();

    /**
     * 获取App平台
     */
    public abstract String getAppPlatform();

    /**
     * 获取App标签
     */
    public abstract String getAppTag();

    /**
     * 获取App名称
     */
    public abstract String getAppName();

    // 获取配置
    protected abstract DesiredCapabilities getCapabilities();

    protected static void SwipeDown(AppiumDriver driver) {
        int width = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        TouchAction action = new TouchAction(driver);
        action.press(PointOption.point(width / 2, height / 2));
        action.waitAction(WaitOptions.waitOptions(Duration.ofMillis(200)));
        action.moveTo(PointOption.point(width / 2, height * 3 / 4));
        action.release();
        action.perform();
    }


    protected static void SwipeUp(AppiumDriver driver) {
        int width = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        TouchAction action = new TouchAction(driver);
        action.press(PointOption.point(width / 2, height / 2));
        action.waitAction(WaitOptions.waitOptions(Duration.ofMillis(200)));
        action.moveTo(PointOption.point(width / 2, height / 4));
        action.release();
        action.perform();
    }
}